//
//  AddDeviceViewController.swift
//  XXIOT
//
//  Created by xx on 2024/8/19.
//

import UIKit
import FastSwiftSDK

class AddDeviceViewController: FastViewController {
    
    private lazy var lbl_tip : FastLabel = {
        let lbl = FastLabel.init(text: "提示：请确认手机已经链接到热点（XXESP_xxxx）",color: themeColor)
        self.view.addSubview(lbl)
        return  lbl
    } ()
    
    private lazy var txt_ssid : FastTextField = {
        let txt = FastTextField.init(color: .x333,placeholder: "WiFi名称")
        self.view.addSubview(txt)
        return  txt
    } ()
    
    private lazy var txt_psd : FastTextField = {
        let txt = FastTextField.init(color: .x333,placeholder: "WiFi密码")
        self.view.addSubview(txt)
        return  txt
    } ()
    
    private lazy var btn_content : FastButton = {
        let btn =  FastButton.init(title: "连接", imgName: "",color: .white ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.backgroundColor = themeColor
        btn.click = { [weak self] in
            self?.content()
        }
        self.view.addSubview(btn)
        return  btn
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "添加设备"
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        lbl_tip.snp.makeConstraints { make in
            make.top.equalTo(50+kNavH)
            make.left.equalTo(20)
        }
        txt_ssid.snp.makeConstraints { make in
            make.top.equalTo(lbl_tip.snp.bottom).offset(50)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(btnHeigth)
        }
        txt_psd.snp.makeConstraints { make in
            make.top.equalTo(txt_ssid.snp.bottom).offset(20)
            make.left.right.height.equalTo(txt_ssid)
        }
        btn_content.snp.makeConstraints { make in
            make.top.equalTo(txt_psd.snp.bottom).offset(50)
            make.left.right.height.equalTo(txt_ssid)
        }
    }
    
    // 设备配网
    func content(){
        
        let vc = DeviceDetailViewController()
        vc.type = "开关"
        vc.mac = "445552223"
        vc.typeName = "开关"
        self.push(vc)
        
        
        let ssid:String = txt_ssid.text ?? ""
        if isEmpty(str: ssid) == true{
            FastHUD.showErr("WiFi名称不能为空")
            return
        }
        let psd:String = txt_psd.text ?? ""
        if isEmpty(str: psd) == true{
            FastHUD.showErr("WiFi密码不能为空")
            return
        }
        
        let urlString = "http://192.168.4.1:80/connect?ssid=\(ssid)&password=\(psd)"
        
        let request = URLRequest(url: URL(string: urlString)!)
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            if let error = error {
                FastHUD.showMoment(error.localizedDescription)
            }
            if let data = data {
                if let res = String(data: data, encoding: .utf8){
                    let cmds = res.components(separatedBy: "/")
                    if cmds.count > 1{
                        if deviceTopics.keys.contains(cmds[0]){
                            FastHUD.showSuc("配网成功")
                            if let dic = deviceTopics.value(Dictionary<String,Any>.self, cmds[0]) {
                                let vc = DeviceDetailViewController()
                                vc.type = cmds[0]
                                vc.mac = cmds[1]
                                vc.typeName = dic.stringConversion("name")
                                self.push(vc)
                            }
                            
                        }else{
                            FastHUD.showErr("设备数据错误")
                        }
                    }else{
                        FastHUD.showErr("设备类型不能识别")
                    }
                }
                FastLog.debug(data)
            }
        }
        
        task.resume()
        
    }
}
